$(function(){
    var n = 0;
    function Plane(plane,pd){
        this.plane = plane;
        this.pd = pd;
    }
    Plane.prototype.fly = function(){
        $(document).on("keydown",function(ev){
            if(ev.keyCode == 37){
                mineFly(-4,0);
            }else if(ev.keyCode == 38){
                mineFly(0,4);
            }else if(ev.keyCode == 39){
                mineFly(4,0);
            }else if(ev.keyCode == 40){
                mineFly(0,-4);
            }
        })
    }
    function mineFly(left,bottom){
        var $mineLeft = parseInt($(".mine").css("left"));
        var $mineBottom = parseInt($(".mine").css("bottom"));
        $(".mine").css({left:$mineLeft+=left});
        $(".mine").css({bottom:$mineBottom+=bottom});
    }
    Plane.prototype.pdFly = function(){
        var that = this;
        $(document).on("keyup",function(ev){
            if(ev.keyCode == 32){
                $(".wrap").append($("<div class='pd'></div>"));
                that.pd = $(".pd");
                var pdLength = that.pd.length-1;
                $(".pd:eq("+pdLength+")").css({left:parseInt(that.plane.css("left"))-5,bottom:parseInt(that.plane.css("bottom"))+50}).attr("type","mine");
                pdMove();
            }
        })
    }
    function pdMove(){
        $(".pd").each(function(idx,item){
            if($(item).attr("type") == "mine"){
                $(item).timer = setInterval(function(){
                    var $itemBottom = parseInt($(item).css("bottom"));
                    $(item).css({bottom:$itemBottom += 8});
                    if(parseInt($(item).css("bottom")) >= $(".wrap").height()){
                        $(item).remove();
                    }
                },30)
            }else{
                $(item).timer = setInterval(function(){
                    var $itemBottom = parseInt($(item).css("bottom"));
                    $(item).css({bottom:$itemBottom -= 8});
                },30)
            }
        })
    }

    function enemyMove(enemy){
        this.enemy = enemy;
    }
    enemyMove.prototype.enemyFly = function(){
        var that = this;
        this.enemy.timer = setInterval(function(){
            var enemyRandom = Math.random()*10;
            if(enemyRandom>9.2){
                enemyAdd()
                this.enemy = $(".enemy");
            }
        },300)
    }

    enemyMove.prototype.fight = function(){
        $(".enemy").each(function(idx,item){
            var enemyLeft = parseInt($(item).css("left"));
            var enemyBottom = parseInt($(item).css("bottom"));
            var mineLeft = parseInt($(".mine").css("left"));
            var mineBottom = parseInt($(".mine").css("bottom"));
            if(mineLeft<enemyLeft+$(item).width() && mineLeft+$(".mine").width()>enemyLeft && mineBottom<enemyBottom+$(item).height()){
                $(".wrap").append($("<div class='pd'></div>"));
                that.pd = $(".pd");
                var pdLength = that.pd.length-1;
                $(".pd:eq("+pdLength+")").css({left:enemyLeft+5,bottom:enemyBottom}).attr("type","enemy");
                pdMove();
            }
        })
    }

    function enemyAdd(){
        $(".wrap").append($("<div class='enemy'></div>"));
        var enemyLength = $(".enemy").length-1;
        $(".enemy:eq("+enemyLength+")").css({bottom:723,left:Math.random()*540});
        $(".enemy:eq("+enemyLength+")").time = setInterval(function(){
            var bottom = parseInt($(".enemy:eq("+n+")").css("bottom"));
            $(".enemy:eq("+n+")").css("bottom",bottom-=5);
            if(parseInt($(".enemy:eq("+n+")").css("bottom")) < -21){
                $(".enemy:eq("+n+")").remove();
            }
        },30)
        n++;
    }

    var playFly = new Plane($(".mine"),$(".pd"));
    playFly.fly();
    playFly.pdFly();

    var enemy = new enemyMove($(".enemy"));
    enemy.enemyFly();
})